package com.company.eos.service.impl;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.eos.domain.AuditLeave;
import com.company.eos.service.AuditDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.company.eos.common.util.PageUtils;
import com.company.eos.mapper.AuditLeaveMapper;
import com.company.eos.service.AuditLeaveService;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 请假申请
 *
 * @author: Song, Wei
 * @date: 2022-03-26
 */
@Slf4j
@Service
public class AuditLeaveServiceImpl extends ServiceImpl<AuditLeaveMapper, AuditLeave>
        implements AuditLeaveService {

    @Autowired
    private AuditLeaveMapper auditLeaveMapper;
    @Autowired
    private AuditDetailService auditDetailService;

    /**
     * 查询请假分页数据
     *
     * @param param 查询携带的参数
     * @return 封装的分页对象
     */
    @Override
    public PageUtils searchLeaveByPage(HashMap param) {
        ArrayList<HashMap> list = auditLeaveMapper.searchLeaveByPage(param);
        long count = auditLeaveMapper.searchLeaveCount(param);

        int start = (Integer) param.get("start");
        int length = (Integer) param.get("length");
        return new PageUtils(list, count, start, length);
    }

    /**
     * 查询请假记录是否与已有记录产生交集
     *
     * @param param 查询请假的参数
     * @return 是否重合
     */
    @Override
    public boolean searchContradiction(HashMap param) {
        long count = auditLeaveMapper.searchContradiction(param);
        return count > 0;
    }

    /**
     * 新增请假记录
     *
     * @param auditLeave 请假审批对象
     * @return 新增的数量
     */
    @Override
    public int insert(AuditLeave auditLeave) {
        return auditLeaveMapper.insert(auditLeave);
    }

    /**
     * 删除请假记录
     *
     * @param param 删除请假的参数
     * @return 删除的数量
     */
    @Override
    public int deleteLeaveById(HashMap param) {
        int id = MapUtil.getInt(param, "id");
        int flowId = auditLeaveMapper.searchFlowIdById(id);
        int rows = auditLeaveMapper.deleteLeaveById(param);
        auditDetailService.deleteAudit(flowId);
        return rows;
    }

    /**
     * 查询请假单
     *
     * @param param 查询请假单的参数
     * @return 请假单数据
     */
    @Override
    public HashMap searchLeaveById(HashMap param) {
        HashMap map = auditLeaveMapper.searchLeaveById(param);
        return map;
    }
}
